package com.ruoyi.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Dongtai;
import com.ruoyi.system.service.IDongtaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/dongtai")
public class DongtaiController {
    @Autowired
    private IDongtaiService dongtaiService;

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Dongtai dongtai) {
        Integer showed = dongtai.getShowed();
        if (Objects.nonNull(showed) && showed != 1) {
            dongtai.setShowed(0);
        }
        Boolean added = dongtaiService.add(dongtai);
        if (added) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        Boolean deleted = dongtaiService.deleteById(id);
        if (deleted) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody Dongtai dongtai) {
        Integer showed = dongtai.getShowed();
        if (Objects.nonNull(showed) && showed != 1) {
            dongtai.setShowed(0);
        }
        Boolean updated = dongtaiService.update(dongtai);
        if (updated) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/get/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        Dongtai dongtai = dongtaiService.getById(id);
        if (dongtai != null) {
            return AjaxResult.success(dongtai);
        } else {
            return AjaxResult.error();
        }
    }
}
